package orest.back.membermanage;

import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.ssta.common.DataBaseConfig;
import net.ssta.common.LoginUser;
import net.ssta.core.base.BaseException;
import net.ssta.core.base.ErrorCode;
import net.ssta.core.base.ServletBase;
import net.ssta.core.db.PagingInfo;
import net.ssta.core.db.Record;
import net.ssta.core.db.RecordSet;
import net.ssta.core.web.RequestKit;

import com.yztc.rc.api.framework.entity.User;
import com.yztc.rc.api.framework.entity.UserAccount;
import com.yztc.rc.api.framework.manager.UserManager;

public class UserPointManager  extends ServletBase {

	private static final long serialVersionUID = 1L;
	 

	public void defaultAction(HttpServletRequest request,HttpServletResponse response) 
	{
		userList(request,response);
	}
 
	public void userList(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		try{ 
			String path=request.getContextPath();
			RequestKit kit=new RequestKit(request);
			Record searchData=kit.getRecordWithPrefix("search_");
			PagingInfo pageingInfo = kit.getPagingInfo(10,"^(?!.*(result)|(LTPAToken)).*$");
			RegisterUserHandler handler=new RegisterUserHandler();
			RecordSet rs=handler.registerList(searchData,pageingInfo);
			request.setAttribute("RECORDSET", rs);
			request.setAttribute("searchData", searchData);
			request.setAttribute("pagingInfo", pageingInfo);
	        this.forward("/back/registerUser/userList.jsp");
	     }catch(Exception se){
		     throw new BaseException(ErrorCode.SERVLET_EXCEPTION, se);
	     }
	}
	
	
	public void resetPwd(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		try{ 
			RequestKit kit=new RequestKit(request);
			String userId=kit.getParameter("userId", "");
			String password=kit.getParameter("password", "");
			
			UserManager um=UserManager.getInstance(DataBaseConfig.getFrameworkDataSource());
			UserAccount  ua=um.getUserAccountByUserId(userId);
			User user=um.getUser(userId);
			ua.setLoginPwd(password);
			um.updateUser(user, ua);
			String path=request.getContextPath();
	        response.sendRedirect(path+"/back/registerUser/resetPwd.jsp?flag=true&userId="+userId);
	     }catch(Exception se){
		     throw new BaseException(ErrorCode.SERVLET_EXCEPTION, se);
	     }
	}
	
	/**
	 * 充值
	 * @param request
	 * @param response
	 * @throws BaseException
	 */
	public void recharge(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		try{ 
			RequestKit kit=new RequestKit(request);
			String userId=kit.getParameter("userId", "");//要充值的用户ID
			String str_money=kit.getParameter("money", "");//要充值的数目
			String mark=kit.getParameter("MARK", "");
			int money=Integer.parseInt(str_money);
			String operatorId=LoginUser.getUserId();//当前操作者的用户ID
			String ipAddress=request.getLocalAddr();//当前操作者的IP
			RegisterUserHandler handler=new RegisterUserHandler();
			handler.recharge(userId,money,operatorId,ipAddress,mark);
			
			String path=request.getContextPath();
	        response.sendRedirect(path+"/back/registerUser/recharge.jsp?flag=true&userId="+userId);
	     }catch(Exception se){
	    	 try{
	    	    response.setCharacterEncoding("GBK");
				PrintWriter   out = response.getWriter();
				 
			    out.print("充值失败，失败原因如下：<br/>"+se);
				out.flush();
				out.close();
	    	 }catch(Exception e ){}
		 //    throw new BaseException(ErrorCode.SERVLET_EXCEPTION, se);
	     }
	}
	
	
	
	
	/**
	 * 扣费
	 * @param request
	 * @param response
	 * @throws BaseException
	 */
	public void chargeBack(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		try{ 
			RequestKit kit=new RequestKit(request);
			String userId=kit.getParameter("userId", "");//要充值的用户ID
			String str_money=kit.getParameter("money", "");//要充值的数目
			String mark=kit.getParameter("MARK", "");
			int money=Integer.parseInt(str_money);
			String operatorId=LoginUser.getUserId();//当前操作者的用户ID
			String ipAddress=request.getLocalAddr();//当前操作者的IP
			RegisterUserHandler handler=new RegisterUserHandler();
			handler.chargeBack(userId,money,operatorId,ipAddress,mark);
			String path=request.getContextPath();
	        response.sendRedirect(path+"/back/registerUser/chargeBack.jsp?flag=true&userId="+userId);
	     }catch(Exception se){
	    	 try{
	    	    response.setCharacterEncoding("GBK");
				PrintWriter   out = response.getWriter();
				 
			    out.print("扣费失败，失败原因如下：<br/>"+se);
				out.flush();
				out.close();
	    	 }catch(Exception e ){}
		 //    throw new BaseException(ErrorCode.SERVLET_EXCEPTION, se);
	     }
	}
	
	
	/**
	 * 统包设置
	 * @param request
	 * @param response
	 * @throws BaseException
	 */
	public void vipConfig(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		try{ 
			RequestKit kit=new RequestKit(request);
			String userId=kit.getParameter("userId", "");//要充值的用户ID
			String str_startDate=kit.getParameter("startDate", "");//开始时间
			String str_endDate=kit.getParameter("endDate", "");//结束时间
			String str_userPoint=kit.getParameter("userPoint", "0");//要扣除的金额
			int userPoint=Integer.parseInt(str_userPoint);
			DateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			Date date=null;
			 
			Date startdate = sdf.parse(str_startDate);
			Date enddate = sdf.parse(str_endDate);
			
			String operatorId=LoginUser.getUserId();//当前操作者的用户ID
			String ipAddress=request.getLocalAddr();//当前操作者的IP
			RegisterUserHandler handler=new RegisterUserHandler();
			
			handler.vipConfig(userId,startdate,enddate,operatorId,ipAddress,userPoint);
			String path=request.getContextPath();
	        response.sendRedirect(path+"/back/registerUser/vipconfig.jsp?flag=true&userId="+userId);
	     }catch(Exception se){
	    	 try{
	    	    response.setCharacterEncoding("GBK");
				PrintWriter   out = response.getWriter();
				 
			    out.print("统包设置失败，失败原因如下：<br/>"+se);
				out.flush();
				out.close();
	    	 }catch(Exception e ){}
		 //    throw new BaseException(ErrorCode.SERVLET_EXCEPTION, se);
	     }
	}
	
	/**
	 * 消费记录
	 * @param request
	 * @param response
	 * @throws BaseException
	 */
	public void costRecordList(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		try{ 
			String path=request.getContextPath();
			RequestKit kit=new RequestKit(request);
			Record searchData=kit.getRecordWithPrefix("search_");
			
			
			String type=kit.getParameter("type", "");
			if("".equals(type)){
				String userId=LoginUser.getUserId();
				searchData.addData("USERID",userId);
			}
			
			
			PagingInfo pageingInfo = kit.getPagingInfo(10,"^(?!.*(result)|(LTPAToken)).*$");
			RegisterUserHandler handler=new RegisterUserHandler();
			RecordSet rs=handler.costRecordList(searchData,pageingInfo);
			request.setAttribute("RECORDSET", rs);
			request.setAttribute("searchData", searchData);
			request.setAttribute("pagingInfo", pageingInfo);
	        
	        if("".equals(type)){
	        	//this.forward("/membercenter/costRecordList.jsp");
	        	this.forwardByStyle("/membercenter/costRecordList.jsp");
			}else{
				this.forward("/back/registerUser/costRecordList.jsp");
			}
	     }catch(Exception se){
		     throw new BaseException(ErrorCode.SERVLET_EXCEPTION, se);
	     }
	}
	
	/**
	 * 充值记录
	 * @param request
	 * @param response
	 * @throws BaseException
	 */
	public void rechargeList(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		try{ 
			String path=request.getContextPath();
			RequestKit kit=new RequestKit(request);
			Record searchData=kit.getRecordWithPrefix("search_");
			
			String type=kit.getParameter("type", "");
			if("".equals(type)){
				String userId=LoginUser.getUserId();
				searchData.addData("USERID",userId);
			}
			
			
			PagingInfo pageingInfo = kit.getPagingInfo(10,"^(?!.*(result)|(LTPAToken)).*$");
			RegisterUserHandler handler=new RegisterUserHandler();
			RecordSet rs=handler.reChargeList(searchData,pageingInfo);
			request.setAttribute("RECORDSET", rs);
			request.setAttribute("searchData", searchData);
			request.setAttribute("pagingInfo", pageingInfo);
			if("".equals(type)){
				//this.forward("/membercenter/rechargeList.jsp");
				 this.forwardByStyle("/membercenter/rechargeList.jsp");
			}else{
				this.forward("/back/registerUser/rechargeList.jsp");
			}
	       
	     }catch(Exception se){
		     throw new BaseException(ErrorCode.SERVLET_EXCEPTION, se);
	     }
	}
	
	
	
	/**
	 * 订单记录
	 * @param request
	 * @param response
	 * @throws BaseException
	 */
	public void orderList(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		try{ 
			String path=request.getContextPath();
			RequestKit kit=new RequestKit(request);
			Record searchData=kit.getRecordWithPrefix("search_");
			
			String type=kit.getParameter("type", "");
			if("".equals(type)){
				String userId=LoginUser.getUserId();
				searchData.addData("USERID",userId);
			}
			
			PagingInfo pageingInfo = kit.getPagingInfo(10,"^(?!.*(result)|(LTPAToken)).*$");
			RegisterUserHandler handler=new RegisterUserHandler();
			RecordSet rs=handler.orderList(searchData,pageingInfo);
			
			
			request.setAttribute("RECORDSET", rs);
			request.setAttribute("searchData", searchData);
			request.setAttribute("pagingInfo", pageingInfo);
			
			if("".equals(type)){
				this.forwardByStyle("/membercenter/orderList.jsp");
			}else{
				this.forward("/back/registerUser/orderList.jsp");
			}
	     }catch(Exception se){
		     throw new BaseException(ErrorCode.SERVLET_EXCEPTION, se);
	     }
	}
	
	
	
	

	/**
	 * 订单记录
	 * @param request
	 * @param response
	 * @throws BaseException
	 */
	public void ipConfig(HttpServletRequest request,HttpServletResponse response) 
	 throws BaseException {
		try{ 
			String path=request.getContextPath();
			RequestKit kit=new RequestKit(request);
			String userId=kit.getParameter("userId", "");
			String[] ip=kit.getParameterValues("IP");
			String[] ipend=kit.getParameterValues("IPEND");
			RegisterUserHandler handler=new RegisterUserHandler();
			handler.ipConfig(userId,ip,ipend);
			
			  response.sendRedirect(path+"/back/registerUser/ipconfig.jsp?flag=true&userId="+userId);
	     }catch(Exception se){
		     throw new BaseException(ErrorCode.SERVLET_EXCEPTION, se);
	     }
	}
	
}
